package net.vinote.smart.platform.service.handler;

import java.sql.SQLException;
import java.util.logging.Level;

import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.DBManagerFactory;
import net.vinote.smart.platform.module.database.DBTransaction;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.module.log.RunLogger;
import net.vinote.smart.platform.service.dw.handler.CommonHandler;
import net.vinote.smart.platform.service.dw.handler.HandlerPerimissionAnnotation;
import net.vinote.smart.platform.service.util.Permission;
import net.vinote.smart.platform.service.util.Permission.PermissionEnum;

@HandlerPerimissionAnnotation(
		insert = @Permission(PermissionEnum.SMART_APP_ITEM_INSERT),
		update = @Permission(PermissionEnum.SMART_APP_ITEM_UPDATE),
		delete = @Permission(PermissionEnum.SMART_APP_ITEM_DELETE),
		query = @Permission(PermissionEnum.SMART_APP_ITEM_QUERY))
public class AppItemHandler extends CommonHandler {

	@Override
	public void initDisplayForAdd(BizObject bo) {
		String appID = request.getParameter("appID");
		setDefaultValue("AppID", appID);
	}

	public void initDisplayForEdit(BizObject bo) {
		setReadOnly("ItemID", true);
	}

	@Override
	protected void beforeDelete(BizObject bo, DBTransaction tx)
			throws SQLException {
		// 清除权限配置信息
		DBManager bm = DBManagerFactory.getInstance("role_object_rela");
		int num = bm.deleteWhereSql("objectType='AppItem' and objectID=?",
				bo.getAttribute("ItemID"));
		RunLogger.getLogger().log(
				Level.FINE,
				"delete AppItem[" + bo.getAttribute("ItemID")
						+ "] roles,effect " + num + " rows");
	}

}
